package com.example.com.myapplication.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.com.myapplication.R;
import com.megvii.facetrack.FaceQualityOption;
import com.megvii.facetrack.FaceTrackOption;
import com.megvii.facetrack.QualityFilter;

/**
 * @author by licheng on 2017/8/15.
 */

public class QualitySettingActivity extends Activity implements View.OnClickListener {


    private EditText edtxtFaceMin;
    private EditText edtxtValuePitch;
    private EditText edtxtValueRoll;
    private EditText edtxtValueYaw;
    private EditText edtxtBrightnessMin;
    private EditText edtxtBrightnessMax;
    private EditText edtxtBrightnessStd;
    private EditText edtxtQuality;

    private RadioButton allowMono;
    private RadioButton unAllowMono;
    private RadioButton allowGlass;
    private RadioButton unAllowGlass;
    private RadioButton allowDarkGlass;
    private RadioButton unAllowDarkGlass;
    private RadioButton allowCloseEye;
    private RadioButton unAllowCloseEye;

    private Button btnSetDefault;
    private Button btnSave;

    SharedPreferences sp;
    SharedPreferences.Editor editor;

    String faceMin;
    String pitch;
    String roll;
    String yaw;
    String brightnessMax;
    String brightnessMin;
    String brightnessStd;
    String quality;

    int faceMinF;
    float pitchF;
    float rollF;
    float yawF;
    float maxF;
    float minF;
    float stdF;
    float qualityF;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quality_setting);
        initViews();
        loadDefaultValue();
    }

    private void initViews() {
        edtxtFaceMin = (EditText) findViewById(R.id.face_min);
        edtxtValuePitch = (EditText) findViewById(R.id.pitch_value);
        edtxtValueRoll = (EditText) findViewById(R.id.roll_value);
        edtxtValueYaw = (EditText) findViewById(R.id.yaw_value);
        edtxtBrightnessMin = (EditText) findViewById(R.id.min_brightness);
        edtxtBrightnessMax = (EditText) findViewById(R.id.max_brightness);
        edtxtBrightnessStd = (EditText) findViewById(R.id.std_brightness);
        edtxtQuality = (EditText) findViewById(R.id.quality);

        allowMono = (RadioButton) findViewById(R.id.mono_allow);
        unAllowMono = (RadioButton) findViewById(R.id.mono_un_allow);
        allowGlass = (RadioButton) findViewById(R.id.glass_allow);
        unAllowGlass = (RadioButton) findViewById(R.id.glass_un_allow);
        allowDarkGlass = (RadioButton) findViewById(R.id.dark_glass_allow);
        unAllowDarkGlass = (RadioButton) findViewById(R.id.dark_glass_un_allow);
        allowCloseEye = (RadioButton) findViewById(R.id.eye_close_allow);
        unAllowCloseEye = (RadioButton) findViewById(R.id.eye_close_un_allow);

        allowMono.setOnCheckedChangeListener(new RadioCheckListener(0));
        unAllowMono.setOnCheckedChangeListener(new RadioCheckListener(1));
        allowGlass.setOnCheckedChangeListener(new RadioCheckListener(2));
        unAllowGlass.setOnCheckedChangeListener(new RadioCheckListener(3));
        allowDarkGlass.setOnCheckedChangeListener(new RadioCheckListener(4));
        unAllowDarkGlass.setOnCheckedChangeListener(new RadioCheckListener(5));
        allowCloseEye.setOnCheckedChangeListener(new RadioCheckListener(6));
        unAllowCloseEye.setOnCheckedChangeListener(new RadioCheckListener(7));

        btnSave = (Button) findViewById(R.id.btn_save);
        btnSetDefault = (Button) findViewById(R.id.btn_set_default);

        btnSave.setOnClickListener(this);
        btnSetDefault.setOnClickListener(this);

        sp = getSharedPreferences("quality_value", Context.MODE_PRIVATE);
        editor = sp.edit();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == btnSave.getId()) {
            if (checkData()) {
                try {
                    saveValues();
                    toMainActivity();
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("save fail", e.getMessage());
                }
            }
        } else if (v.getId() == btnSetDefault.getId()) {
            edtxtFaceMin.setText("" + FaceQualityOption.DEFAULT_VALUE_FACE_MIN);
            edtxtValuePitch.setText("" + FaceQualityOption.DEFAULT_VALUE_PITCH);
            edtxtValueRoll.setText("" + FaceQualityOption.DEFAULT_VALUE_ROLL);
            edtxtValueYaw.setText("" + FaceQualityOption.DEFAULT_VALUE_YAW);

            edtxtBrightnessMax.setText("" + FaceQualityOption.DEFAULT_VALUE_BRIGHT_MAX);
            edtxtBrightnessMin.setText("" + FaceQualityOption.DEFAULT_VALUE_BRIGHT_MIN);
            edtxtBrightnessStd.setText("" + FaceQualityOption.DEFAULT_VALUE_BRIGHT_STD);
            edtxtQuality.setText("" + FaceQualityOption.DEFAULT_VALUE_QUALITY);

            allowMono.setChecked(false);
            unAllowMono.setChecked(true);

            allowGlass.setChecked(true);
            unAllowGlass.setChecked(false);

            allowDarkGlass.setChecked(false);
            unAllowDarkGlass.setChecked(true);

            allowCloseEye.setChecked(true);
            unAllowCloseEye.setChecked(false);
        }
    }

    private void saveValues() {
        editor.putInt(QualityFilter.KEY_FACE_MIN, Integer.valueOf(faceMin)).apply();
        editor.putInt(QualityFilter.KEY_PITCH, Integer.valueOf(pitch)).apply();
        editor.putInt(QualityFilter.KEY_ROLL, Integer.valueOf(roll)).apply();
        editor.putInt(QualityFilter.KEY_YAW, Integer.valueOf(yaw)).apply();
        editor.putInt(QualityFilter.KEY_BRIGHT_MAX, Integer.valueOf(brightnessMax)).apply();
        editor.putInt(QualityFilter.KEY_BRIGHT_MIN, Integer.valueOf(brightnessMin)).apply();
        editor.putInt(QualityFilter.KEY_BRIGHT_STD, Integer.valueOf(brightnessStd)).apply();
        editor.putFloat(QualityFilter.KEY_QUALITY, Float.valueOf(quality)).apply();
        // 更新filter
        QualityFilter.update();
    }

    private void toMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        FaceTrackOption.Builder builder = new FaceTrackOption.Builder();
        FaceQualityOption qualityOption = new FaceQualityOption();
        qualityOption.setFaceMin(faceMinF);
        qualityOption.setPitch(pitchF / 100);
        qualityOption.setRoll(rollF / 100);
        qualityOption.setYaw(yawF / 100);
        qualityOption.setBrightMax(maxF);
        qualityOption.setBrightMin(minF);
        qualityOption.setBrightStd(stdF);
        qualityOption.setQuality(qualityF);
        qualityOption.setMono(allowMono.isChecked());
        qualityOption.setGlass(allowGlass.isChecked());
        qualityOption.setDarkGlass(allowDarkGlass.isChecked());
        qualityOption.setCloseEye(allowCloseEye.isChecked());

        builder.setQualityOption(qualityOption);
        FaceTrackOption option = builder.build();
        intent.putExtra(MainActivity.OPTION, option);
        startActivity(intent);
    }

    private boolean checkData() {
        faceMin = edtxtFaceMin.getText().toString();
        pitch = edtxtValuePitch.getText().toString();
        roll = edtxtValueRoll.getText().toString();
        yaw = edtxtValueYaw.getText().toString();
        brightnessMax = edtxtBrightnessMax.getText().toString();
        brightnessMin = edtxtBrightnessMin.getText().toString();
        brightnessStd = edtxtBrightnessStd.getText().toString();
        quality = edtxtQuality.getText().toString();
        if (TextUtils.isEmpty(pitch) || TextUtils.isEmpty(roll) || TextUtils.isEmpty(yaw)
                || TextUtils.isEmpty(brightnessMax) || TextUtils.isEmpty(brightnessMin)
                || TextUtils.isEmpty(brightnessStd) || TextUtils.isEmpty(quality)) {
            Toast.makeText(this, "任何参数不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }

        faceMinF = Integer.parseInt(faceMin);
        pitchF = Float.parseFloat(pitch);
        rollF = Float.parseFloat(roll);
        yawF = Float.parseFloat(yaw);
        maxF = Float.parseFloat(brightnessMax);
        minF = Float.parseFloat(brightnessMin);
        stdF = Float.parseFloat(brightnessStd);
        qualityF = Float.parseFloat(quality);

        if (faceMinF < FaceQualityOption.DEFAULT_VALUE_FACE_MIN || 0 >= faceMinF) {
            Toast.makeText(this, "请设置最小脸尺寸合法值", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (pitchF > FaceQualityOption.DEFAULT_VALUE_PITCH || 0 >= pitchF) {
            Toast.makeText(this, "请设置点头角度合法值", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (rollF > FaceQualityOption.DEFAULT_VALUE_ROLL || 0 >= rollF) {
            Toast.makeText(this, "请设置歪头角度合法值", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (yawF > FaceQualityOption.DEFAULT_VALUE_YAW || 0 >= yawF) {
            Toast.makeText(this, "请设置摇头角度合法值", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (maxF > FaceQualityOption.DEFAULT_VALUE_BRIGHT_MAX || 0 >= maxF || maxF <= minF) {
            Toast.makeText(this, "请设置最大亮度合法值", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (minF < FaceQualityOption.DEFAULT_VALUE_BRIGHT_MIN || 0 >= minF || minF >= maxF) {
            Toast.makeText(this, "请设置最小亮度合法值", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (stdF > FaceQualityOption.DEFAULT_VALUE_BRIGHT_STD || 0 >= stdF) {
            Toast.makeText(this, "请设置标准差合法值", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (0 > Float.compare(qualityF, FaceQualityOption.DEFAULT_VALUE_QUALITY) || 0 >= qualityF) {
            Toast.makeText(this, "请设置模糊值合法值", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private class RadioCheckListener implements CompoundButton.OnCheckedChangeListener {

        private int radioId;

        public RadioCheckListener(int radioId) {
            this.radioId = radioId;
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                switch (radioId) {
                    case 0: // 允许黑白
                        editor.putBoolean(QualityFilter.KEY_MONO, true);
                        break;
                    case 1: // 禁止黑白
                        editor.putBoolean(QualityFilter.KEY_MONO, false);
                        break;
                    case 2: // 允许戴眼镜
                        editor.putBoolean(QualityFilter.KEY_GLASS, true);
                        break;
                    case 3: // 禁止戴眼镜
                        editor.putBoolean(QualityFilter.KEY_GLASS, false);
                        break;
                    case 4: // 允许戴墨镜
                        editor.putBoolean(QualityFilter.KEY_DARK_GLASS, true);
                        break;
                    case 5: // 禁止戴墨镜
                        editor.putBoolean(QualityFilter.KEY_DARK_GLASS, false);
                        break;
                    case 6: // 允许闭眼
                        editor.putBoolean(QualityFilter.KEY_CLOSE_EYE, true);
                        break;
                    case 7: // 禁止闭眼
                        editor.putBoolean(QualityFilter.KEY_CLOSE_EYE, false);
                        break;
                }
                editor.apply();
                // 更新filter
                QualityFilter.update();
            }
        }
    }

    private void loadDefaultValue() {
        // 最小脸尺寸
        edtxtFaceMin.setText("" + sp.getInt(QualityFilter.KEY_FACE_MIN, FaceQualityOption.DEFAULT_VALUE_FACE_MIN));
        // 人脸
        edtxtValuePitch.setText("" + sp.getInt(QualityFilter.KEY_PITCH, FaceQualityOption.DEFAULT_VALUE_PITCH));
        edtxtValueRoll.setText("" + sp.getInt(QualityFilter.KEY_ROLL, FaceQualityOption.DEFAULT_VALUE_ROLL));
        edtxtValueYaw.setText("" + sp.getInt(QualityFilter.KEY_YAW, FaceQualityOption.DEFAULT_VALUE_YAW));

        // 亮度
        edtxtBrightnessMax.setText("" + sp.getInt(QualityFilter.KEY_BRIGHT_MAX, FaceQualityOption.DEFAULT_VALUE_BRIGHT_MAX));
        edtxtBrightnessMin.setText("" + sp.getInt(QualityFilter.KEY_BRIGHT_MIN, FaceQualityOption.DEFAULT_VALUE_BRIGHT_MIN));
        edtxtBrightnessStd.setText("" + sp.getInt(QualityFilter.KEY_BRIGHT_STD, FaceQualityOption.DEFAULT_VALUE_BRIGHT_STD));

        // 模糊度
        edtxtQuality.setText("" + sp.getFloat(QualityFilter.KEY_QUALITY, FaceQualityOption.DEFAULT_VALUE_QUALITY));

        // 允许闭眼
        if (sp.getBoolean(QualityFilter.KEY_CLOSE_EYE, FaceQualityOption.DEFAULT_CLOSE_EYE)) {
            allowCloseEye.setChecked(true);
            unAllowCloseEye.setChecked(false);
        } else {
            allowCloseEye.setChecked(false);
            unAllowCloseEye.setChecked(true);
        }

        // 戴眼镜
        if (sp.getBoolean(QualityFilter.KEY_GLASS, FaceQualityOption.DEFAULT_VALUE_GLASS)) {
            allowGlass.setChecked(true);
            unAllowGlass.setChecked(false);
        } else {
            allowGlass.setChecked(false);
            unAllowGlass.setChecked(true);
        }

        // 戴墨镜
        if (sp.getBoolean(QualityFilter.KEY_DARK_GLASS, FaceQualityOption.DEFAULT_VALUE_DARK_GLASS)) {
            allowDarkGlass.setChecked(true);
            unAllowDarkGlass.setChecked(false);
        } else {
            allowDarkGlass.setChecked(false);
            unAllowDarkGlass.setChecked(true);
        }

        // 黑白照片
        if (sp.getBoolean(QualityFilter.KEY_MONO, FaceQualityOption.DEFAULT_VALUE_MONO)) {
            allowMono.setChecked(true);
            unAllowMono.setChecked(false);
        } else {
            allowMono.setChecked(false);
            unAllowMono.setChecked(true);
        }

    }
}
